package com.we823.project.controller.report;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.we823.project.common.web.status.PermissionStatus;
import com.we823.project.controller.AbstractBaseController;
import com.we823.project.core.web.WebConstants;
import com.we823.project.model.BaseModel;
import com.we823.project.repository.service.BaseService;
import com.we823.utils.web.DateUtil;
import com.we823.utils.web.RequestUtil;

public abstract class AbstractBaseReportController<T extends BaseModel> extends AbstractBaseController<T> {

	public AbstractBaseReportController(BaseService<T, Integer> baseService) {
		super(baseService);
	}

	@RequestMapping("getInfo")
	@ResponseBody
	public Model getInfo(HttpServletRequest request, Model model) {
		
		check(PermissionStatus.VIEW);
		model.addAttribute("year",DateUtil.getYear());
		model.addAttribute("currentYear",DateUtil.getYear());
		
		model.addAttribute("currentMonth",DateUtil.getMonth());
		model.addAttribute("currentDay",DateUtil.getDay());
		
		model.addAttribute("day",DateUtil.getMaxDay(DateUtil.getYear(), DateUtil.getMonth()));
		expendInfo(model);
		return model;
	}

	/**
	 * 添加额外的信息
	 * @param model
	 */
	public abstract void expendInfo(Model model);

	@RequestMapping("download")
	@ResponseBody
	public Model download(HttpServletRequest request,HttpServletResponse response, Model model) throws IOException {
		check(PermissionStatus.CREATE);
		String filename = RequestUtil.getValueByParam(request, "filename");
		String sheetname = RequestUtil.getValueByParam(request, "sheetname");
		if(filename!=null) {
			int index = filename.lastIndexOf(".");
			if (index>0) {
				String tempFilename = StringUtils.lowerCase(filename);
				String tempSheetname = StringUtils.lowerCase(sheetname);
				String tempext = StringUtils.substring(tempFilename,index);
				if(!tempSheetname.endsWith(tempext)) {
					String ext = StringUtils.substring(filename, index);
					sheetname+=ext;
				}
			}
			String fullname = WebConstants.EXCEL_PATH + filename;
			response.addHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(sheetname,"utf-8"));
			RequestDispatcher dispatcher = request.getRequestDispatcher(fullname);
			try {
				dispatcher.forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				response.flushBuffer();
			}
		}
		return model;
	}

	@RequestMapping("exportExcel")
	@ResponseBody
	public Model exportExcel(HttpServletRequest request, Model model) {
		check(PermissionStatus.CREATE);
		Page<T> contentPage = getData(null, request, model, false);
		if (contentPage!=null) {
			List<T> models = contentPage.getContent();
			
			String sheetname = RequestUtil.getValueByParam(request, "caption","报表");
			String filename = DateUtil.getDateString()+".xlsx";
			String path = WebConstants.EXCEL_PATH;
			ServletContext servletContext = request.getServletContext();
			String fullpath = servletContext.getRealPath(path);
			File filepath = new File(fullpath);
			if(!filepath.exists()) {
				filepath.mkdirs();
			}
			
			String fullname = fullpath +"/"+ filename;
			boolean result = export(fullname, sheetname, models);
			
			if(result) {
				model.addAttribute("result",result);
				model.addAttribute("filename",filename);
				model.addAttribute("sheetname",sheetname);
			}else {
				model.addAttribute("result",false);
				model.addAttribute("message","生成文件出现错误");
			}
		}else {
			model.addAttribute("result",false);
			model.addAttribute("message","记录为空");
		}
		return model;
	}

	public abstract boolean export(String fullname, String sheetname, List<T> models);
}
